﻿using System;
using ZFine.Application.Gas;
using ZFine.Application.SystemManage;
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.Entity.SystemManage;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace ZFine.Web.Areas.Gas.Controllers
{
    public class ChargeRecordController : ControllerBase
    {
        private ChargeRecordApp chargeRecordApp = new ChargeRecordApp();
        private DispenserApp dispenserApp = new DispenserApp();
        private CompanyApp companyApp = new CompanyApp();
        private BottleApp bottleApp = new BottleApp();
        private UserApp userApp = new UserApp();
        [HttpGet]
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Form()
        {
            return View();
        }
        [HandlerAuthorize(ignore: false)]
        public override ActionResult Details()
        {
            return View();
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(Pagination pagination, string keyword)
        {
            List<ChargeRecordEntity> l = chargeRecordApp.GetList(pagination, keyword);
            List<UserEntity> userEntities = userApp.GetList();
            //因为有部分车牌没有记录，需要使用左连接

            var rows =( from chargerecord in l
                       join dispenser in dispenserApp.GetIQueryable()
                       on chargerecord.DispenserId equals dispenser.F_Id
                       join company in companyApp.GetIQueryable()
                       on dispenser.CompanyId equals company.F_Id


                       join user_a_left in userEntities
                       on chargerecord.AscertainId equals user_a_left.F_Id into t1
                       from user_a in t1.DefaultIfEmpty()

                       join user_c in userEntities
                       on chargerecord.CheckId equals user_c.F_Id into t2
                       from user_c in t2.DefaultIfEmpty()

                       join user_d in userEntities
                       on chargerecord.DealId equals user_d.F_Id into t3
                       from user_d in t3.DefaultIfEmpty()

                       select new
                       {
                           chargerecord.F_Id,
                           dispenser.DeviceName,
                           company.CompanyName,
                           chargerecord.PlateNumber,
                           chargerecord.ChargeType,
                           ChargeDate= (chargerecord.ChargeDate == PubClass.MinDate() ? "" : chargerecord.ChargeDate.ToString(PubClass.TimeFormat)),
                           chargerecord.RecordStatus,
                           AscertainName = (user_a==null)?"":user_a.F_RealName,
                           AscertainDate =(chargerecord.AscertainDate==PubClass.MinDate()?"": chargerecord.AscertainDate.ToString(PubClass.TimeFormat)),
                           CheckName = (user_c == null) ? "" : user_c.F_RealName,
                           CheckDate = (chargerecord.CheckDate == PubClass.MinDate() ? "" : chargerecord.CheckDate.ToString(PubClass.TimeFormat)),
                           DealName = (user_d == null) ? "" : user_d.F_RealName,
                           DealDate = (chargerecord.DealDate == PubClass.MinDate() ? "" : chargerecord.DealDate.ToString(PubClass.TimeFormat)),
                       }).OrderByDescending(a=>a.ChargeDate).ToList();

            var data = new
            {
                page = pagination.page,//当前页  1  2 3 之类
                records = pagination.records,//总条数  这里应该是18
                rows = rows,//分页的数据
                total = pagination.total,//页数
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonByTypeAndStatus(Pagination pagination, string keyword,string chargeType,string recordStatus)
        {
            List<ChargeRecordEntity> l = chargeRecordApp.GetList(pagination, keyword, chargeType, recordStatus);
            List<UserEntity> userEntities = userApp.GetList();

            var rows = (from chargerecord in l
                        join dispenser in dispenserApp.GetIQueryable()
                        on chargerecord.DispenserId equals dispenser.F_Id
                        join company in companyApp.GetIQueryable()
                        on dispenser.CompanyId equals company.F_Id


                        join user_a_left in userEntities
                        on chargerecord.AscertainId equals user_a_left.F_Id into t1
                        from user_a in t1.DefaultIfEmpty()

                        join user_c in userEntities
                        on chargerecord.CheckId equals user_c.F_Id into t2
                        from user_c in t2.DefaultIfEmpty()

                        join user_d in userEntities
                        on chargerecord.DealId equals user_d.F_Id into t3
                        from user_d in t3.DefaultIfEmpty()

                        select new
                        {
                            chargerecord.F_Id,
                            dispenser.DeviceName,
                            company.CompanyName,
                            chargerecord.PlateNumber,
                            chargerecord.ChargeType,
                            ChargeDate = (chargerecord.ChargeDate == PubClass.MinDate() ? "" : chargerecord.ChargeDate.ToString(PubClass.TimeFormat)),
                            chargerecord.RecordStatus,
                            AscertainName = (user_a == null) ? "" : user_a.F_RealName,
                            AscertainDate = (chargerecord.AscertainDate == PubClass.MinDate() ? "" : chargerecord.AscertainDate.ToString(PubClass.TimeFormat)),
                            CheckName = (user_c == null) ? "" : user_c.F_RealName,
                            CheckDate = (chargerecord.CheckDate == PubClass.MinDate() ? "" : chargerecord.CheckDate.ToString(PubClass.TimeFormat)),
                            DealName = (user_d == null) ? "" : user_d.F_RealName,
                            DealDate = (chargerecord.DealDate == PubClass.MinDate() ? "" : chargerecord.DealDate.ToString(PubClass.TimeFormat)),
                        }).ToList();


            var data = new
            {
                rows = rows,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            ChargeRecordEntity r = chargeRecordApp.GetForm(keyValue);
            DispenserEntity d = dispenserApp.GetForm(r.DispenserId);
            CompanyEntity c = companyApp.GetForm(d.CompanyId);

            var data = new
            {
                r.PlateNumber,
                c.CompanyName,
                r.ChargeDate,
                PlateNumber_Pic=string.Format("/upload/GasClient/camera/images/{0}", r.PlateNumber_Pic),
                VideoFile=string.Format("/upload/GasClient/camera/video/{0}", r.VideoFile)
            };
            return Content(data.ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(ChargeRecordEntity ChargeRecordEntity, string permissionIds, string keyValue)
        {
            chargeRecordApp.SubmitForm(ChargeRecordEntity, keyValue);
            return Success("操作成功。");
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            chargeRecordApp.DeleteForm(keyValue);
            return Success("删除成功。");
        }
        /// <summary>
        /// 待落实列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        //[HandlerAuthorize]
        public virtual ActionResult Ascertain()
        {
            return View();
        }
        /// <summary>
        /// 提交复核
        /// </summary>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitCheck(string recordid)
        {
            chargeRecordApp.SubmitCheck(recordid);
            return Success("提交复核成功。");
        }
        /// <summary>
        /// 消除误报
        /// </summary>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult CancelReport(string recordid)
        {
            chargeRecordApp.CancelReport(recordid);
            return Success("消除误报成功。");
        }
        /// <summary>
        /// 申请决定意见
        /// </summary>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult AskView(string recordid)
        {
            chargeRecordApp.AskView(recordid);
            return Success("申请决定意见成功。");
        }
        /// <summary>
        /// 驳回
        /// </summary>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult Reject(string recordid)
        {
            chargeRecordApp.Reject(recordid);
            return Success("驳回成功。");
        }
        /// <summary>
        /// 处罚
        /// </summary>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult Punish(string recordid)
        {
            chargeRecordApp.Punish(recordid);
            return Success("处罚成功。");
        }
        /// <summary>
        /// 处理消除
        /// </summary>
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult Clear(string recordid)
        {
            chargeRecordApp.Clear(recordid);
            return Success("处理消除成功。");
        }


        [HttpGet]
        //[HandlerAuthorize]
        public virtual ActionResult Check()
        {
            return View();
        }
        [HttpGet]
        //[HandlerAuthorize]
        public virtual ActionResult Deal()
        {
            return View();
        }
        [HttpGet]
        //[HandlerAuthorize]
        public virtual ActionResult Images()
        {
            return View();
        }
    }
}
